#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <math.h>
#include <pthread.h>


#define MAX_NUM_Client 100
#define MAX_NUM_EPOLL 0

// void tasks_init(){
//     pthread_t send_pthread[MAX_NUM_EPOLL];
//     for(int i=0;i<MAX_NUM_EPOLL;i++){
//         pthread_create(&send_pthread[i],NULL,other_pthread,NULL);
//         pthread_detach(send_pthread[i]);
//     }
// }

// void* other_pthread(void* arg){
   
// }
int main(int argc, char *argv[])
{
    int random_num;
    int sockfds[MAX_NUM_Client];
    for(int i=0;i<MAX_NUM_Client;i++){
        sockfds[i]=socket( AF_INET, SOCK_STREAM, 0 );
        if ( -1 == sockfds[i] ) {
            perror( "sock created" );
            exit( -1 );
        }
    }
    
    
    //初始化服务器接口
    struct sockaddr_in server;
    memset( &server, 0, sizeof( struct sockaddr_in ) );
    server.sin_family = AF_INET;
    server.sin_port = htons(2400);
    server.sin_addr.s_addr = inet_addr( "127.0.0.1" );

    int res[MAX_NUM_Client];

    for(int i=0;i<MAX_NUM_Client;i++){
        res[i] = connect( sockfds[i], (struct sockaddr*)&server, sizeof( server ) );
        if( -1 == res[i] ){
            perror( "sock connect" );
            exit( -1 );
        }
    }
    
    char sendBuf[4100] = { 0 };
    char recvBuf[4100];
    while( 1 ) {
        random_num=rand()%MAX_NUM_Client;
        printf("send message=");
        fgets( sendBuf, sizeof( sendBuf ), stdin );
        if(sendBuf[0]=='0'){
            break;
        }
        write( sockfds[random_num], sendBuf, sizeof( sendBuf ) );
        read( sockfds[random_num], recvBuf, sizeof( recvBuf ) );
        printf("recv from server=%s",recvBuf,sizeof(recvBuf));
        memset( sendBuf, 0, sizeof( sendBuf ) );
        memset( recvBuf, 0, sizeof( recvBuf ) );
    }

    for(int i=0;i<MAX_NUM_Client;i++){
       close( sockfds[i] );
    }
    return 0;
}